/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.dao;

import com.home.common.utils.HibernateUtil;
import com.home.mapping.Scheme;
import com.home.mapping.Venture;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author pnduy
 */
public class SchemeDao extends BaseDao{
    /**
     * 
     * @param idScheme
     * @return 
     */
    public Scheme findSchemeById(Integer idScheme) {
        Scheme scheme = new Scheme();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Scheme.class);
            criteria.add(Restrictions.eq("id_scheme",idScheme));
            
            List result = criteria.list();
            if (result.size() > 0) {
                scheme = (Scheme)result.get(0);                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return scheme;
    }
    
    /**
     * 
     * @return 
     */
    public List<Scheme> findSchemesByType(Integer type) {
        List<Scheme> list = new ArrayList<Scheme>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Scheme.class);
            criteria.add(Restrictions.eq("type",type));
            list = criteria.list();
            
        } catch (Exception e) {
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return list;
    }
    
    public List<Scheme> findAllScheme() {
        List<Scheme> list = new ArrayList<Scheme>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Scheme.class);
            
            list = criteria.list();
            
        } catch (Exception e) {
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return list;
    }
}
